#函数实例

add <- function(x,y){
  z <- x+y
  return(z)
}

a <- add(1,2)

#循环

for(i in 1:10){
  print(2*i)
}

#10L 表示十进制的10
library(stringr)
?str_c

#定义安装包函数
install <- function(package){ 
  packages <- c(package) 
  if (length(setdiff(packages, rownames(installed.packages()))) > 0) { 
    install.packages(setdiff(packages, rownames(installed.packages())))  
  }else{
    print(paste(packages,"is","installed","already.",sep=" "))
  }
}

install("RMySQL")
 
#链接数据库
library("RMySQL")
help(package="RMySQL")

MySQL()#<MySQLDriver>
con <- dbConnect(MySQL(),
                 host="127.0.0.1",
                 port=3307,
                 user="root",
                 password="root",
                 dbname="world")
sql <- "SELECT * FROM city"
city <- dbGetQuery(con,sql)
#关闭数据库
dbDisconnect(con)





















